#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
#endregion

namespace PHSRAG.Insight21.Humans
{
	/// <summary>
	/// Represents a board review status phase a protocol submission has undergone
	/// </summary>
	public class Review : BO.BusinessObject
	{

		#region Constants
		private const string ReviewKey = "Review_8D5FF6CE-6E67-4baf-BC69-56312E9A6825";
		private const string ReviewTableName = "Review";
		#endregion

		#region Instance Variables
		private DataRow	dataRow;
		private Submission parent;
		#endregion

		#region Public Properties

		/// <summary>
		/// Indicates what number in a sequence of submissions (of the same type) the parent submission 
		/// is (e.g. if the parent submission is the second amendment, this returns 2)
		/// </summary>
		public string FormsId
		{
			get
			{
				return (string)dataRow["FormsId"];
			}
		}

		/// <summary>
		/// For an adverse event, the id of the subject experiencing the adverse event 
		/// </summary>
		public string SubjectId
		{
			get
			{
				object o = dataRow["SubjectId"];
				return (o == DBNull.Value) ? string.Empty : (string)o;
			}
		}
			
		/// <summary>
		/// Gets the Id of the record in the ProtocolBoardReview table in the database
		/// </summary>
		public int BoardReviewId
		{
			get
			{
				return (int)dataRow["Id"];
			}
		}
			
		/// <summary>
		/// Gets the submission object that the review status is based upon
		/// </summary>
		public Submission Parent
		{
			get
			{
				return parent;
			}
		}

		/// <summary>
		/// Gets the name of the board that has/will be reviewing the protocol if it exists, 
		/// otherwise it returns an empty string
		/// </summary>
		public string BoardName
		{
			get
			{
				object o = dataRow["BoardName"];
				return (o == DBNull.Value) ? string.Empty : (string)o;
			}
		}

		/// <summary>
		/// Gets the type of other event, if it exists (otherwise returns empty string)
		/// </summary>
		public string OtherEventType
		{
			get
			{
				object o = dataRow["OtherEventType"];
				return (o == DBNull.Value) ? string.Empty : (string)o;
			}
		}

		/// <summary>
		/// Gets the type of the review (e.g. Full, Expedited, Exempt)
		/// </summary>
		public string ReviewType
		{
			get
			{ 
				object o = dataRow["ReviewType"];
				return (o == DBNull.Value) ? string.Empty : (string)o;
			}
		}

		/// <summary>
		/// Gets the Identifier assigned by the corporate sponsor
		/// </summary>
		public string SponsorAmendmentNumber
		{
			get
			{ 
				object o = dataRow["SponsorAmendmentNumber"];
				return (o == DBNull.Value) ? string.Empty : (string)o;
			}
		}

		/// <summary>
		/// Gets the date the submission will be reviewed if it exists, otherwise
		/// returns an empty string
		/// </summary>
		public string MeetingDate
		{
			get
			{ 
				object o = dataRow["MeetingDate"];
				return (o == DBNull.Value) ? string.Empty : ((DateTime)o).ToString("MM/dd/yyyy");
			}		
		}

		/// <summary>
		/// Gets the date the IRB received a response from the PI if it exists, otherwise
		/// returns an empty string
		/// </summary>
		public string ResponseDate
		{
			get
			{ 
				object o = dataRow["ResponseDate"];
				return (o == DBNull.Value) ? string.Empty : ((DateTime)o).ToString("MM/dd/yyyy");
			}		
		}

		/// <summary>
		/// Gets the date the submission was received
		/// </summary>
		public string ReceivedDate
		{
			get
			{ 
				object o = dataRow["DateReceived"];
				return (o == DBNull.Value) ? string.Empty : ((DateTime)o).ToString("MM/dd/yyyy");
			}		
		}

		/// <summary>
		/// Gets the date the status on the submission changed
		/// </summary>
		public string StatusModifiedDate
		{
			get
			{ 
				object o = dataRow["StatusModifiedDate"];
				return (o == DBNull.Value) ? string.Empty : ((DateTime)o).ToString("MM/dd/yyyy");
			}		
		}

		/// <summary>
		/// Gets the status of this board review record
		/// </summary>
		public string Status
		{
			get
			{
				return (string) dataRow["Status"];
			}
		}

		/// <summary>
		/// Gets the project status if it exists, otherwise returns an empty string
		/// </summary>
		public string ProjectStatus
		{
			get
			{
				object o = dataRow["ProjectStatus"];
				return (o == DBNull.Value) ? "" : (string)o;
			}
		}

		/// <summary>
		/// Gets the status of this board review record if it's a continuing review
		/// </summary>
		public string CRStatus
		{
			get
			{
				object o = dataRow["CRStatus"]; 
				return (o == DBNull.Value) ? string.Empty : (string)o;
			}
		}

		/// <summary>
		/// Returns true if there is notification text on the submission
		/// </summary>
		public bool Notification
		{
			get
			{
				return Convert.ToBoolean(dataRow["Notification"]);
			}
		}

		/// <summary>
		/// Gets Protocol Process ID from submission
		/// </summary>
		public int ProtocolProcessId
		{
			get
			{
				object o = dataRow["ProtocolProcessId"];
				return (o == DBNull.Value) ? 0 : (int)o;				
			}
		}
        /// <summary>
        /// Gets the status of this board review record if it's a continuing review
        /// </summary>
        public int NotificationLetterId
        {
            get
            {
                object o = dataRow["NotificationLetterId"];
                return (o == DBNull.Value) ? 0 : (int)o;
            }
        }

		#endregion 

		#region Constructors

		/// <summary>
		/// Creates a review object, initializes the dataRow to point to the row in the
		/// parent Submission object's dataContainer, and sets the parent reference to the 
		/// one passed in
		/// </summary>
		/// </summary>
		/// <param name="cachePolicy">policy to use to cache this instance</param>
		/// <param name="row">the row that stores the data relevant to this instance</param>
		/// <param name="parent">the parent submission object that contains the row</param>
		public Review(ICachePolicy cachePolicy, DataRow row, Submission parent) : base(cachePolicy)
		{
			dataRow = row;
			this.parent = parent;
		}

		#endregion Constructors

		#region Overrides

		/// <summary>
		/// Loads the datacontainer with the review data from the database
		/// </summary>
		/// <param name="connectionString"></param>
		/// <param name="args"></param>
		public override void Load(string connectionString, params object[] args)
		{	
			throw new Exception(
				"Review is not meant to be loaded using this method, pass a DataRow to the constructor instead");	
		}

		/// <summary>
		/// Adds the review object to the cache
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(ReviewKey, this);
		}

		/// <summary>
		/// Removes the review object from the cache
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(ReviewKey);	
		}

		#endregion Overrides
	}
}

